package com.zhangli.mytoolkit;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.widget.Toast;

import com.zhangli.communication.email.Address;
import com.zhangli.communication.email.Content;
import com.zhangli.communication.email.Receive;
import com.zhangli.communication.email.Send;
import com.zhangli.config.Config;
import com.zhangli.media.MyAudioRecord;

import java.io.File;

import android.os.AsyncTask;

/**
 * Created by zhangli on 2017/8/19.
 */

public class Checker extends  AsyncTask<Integer, Integer, String>{

    Context context;
    public Checker(Context context)
    {
        super();
        this.context = context;
    }
    private static String ReceiveEmail() {
        Config p = Config.getInstance();
        Address sendMailModel = new Address();
        //sendMailModel.setTo("zhangli_java@163.com");
        //sendMailModel.setFrom("zhangli_java@163.com");
        sendMailModel.setUsername(p.readString("Username",""));
        sendMailModel.setPassword(p.readPassword("Password"));
        //sendMailModel.setPort("465");
        sendMailModel.setPort("110");
        sendMailModel.setHost("pop3.163.com");
        return Receive.receiveLastEmailForTitle(sendMailModel);
    }

    private static void SendEmail(String filepath) {
        Config p = Config.getInstance();
        Address sendMailModel = new Address();
        sendMailModel.setTo(p.readString("EmailTo",""));
        sendMailModel.setFrom(p.readString("EmailFrom",""));
        sendMailModel.setUsername(p.readString("Username",""));
        sendMailModel.setPassword(p.readPassword("Password"));
        sendMailModel.setPort("25");
        sendMailModel.setHost("smtp.163.com");


        Content mailModel = new Content();
        mailModel.setSubject("My Tool Kit");
        mailModel.setText("My Tool Kit");
        mailModel.setFile(filepath);
        Send.sendAttachmentInEmail(sendMailModel, mailModel);
    }

    /**
     * 这里的Integer参数对应AsyncTask中的第一个参数
     * 这里的String返回值对应AsyncTask的第三个参数
     * 该方法并不运行在UI线程当中，主要用于异步操作，所有在该方法中不能对UI当中的空间进行设置和修改
     * 但是可以调用publishProgress方法触发onProgressUpdate对UI进行操作
     */
    @Override
    protected String doInBackground(Integer... params) {
        String title = ReceiveEmail();

        //Toast.makeText(context, "time trigger:" + title, Toast.LENGTH_SHORT).show();

        String outputFile = Environment.getExternalStorageDirectory().
                getAbsolutePath() + "/MyToolKit";

        File file = new File(outputFile);
        if (!file.exists()) {
            file.mkdirs();
        }

        outputFile = outputFile + "/" + "record.3gp";
        try {
            switch (title) {

                case "#start": {
                    Intent i = new Intent(context, MyAudioRecord.class);
                    i.putExtra("Command", "start");
                    i.putExtra("path", outputFile);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startService(i);
                    break;
                }
                case "#stop": {
                    Intent i = new Intent(context, MyAudioRecord.class);
                    i.putExtra("Command", "stop");
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startService(i);

                    SendEmail(outputFile);
                    break;
                }
                default: {
                    Intent i = new Intent(context, MyAudioRecord.class);
                    i.putExtra("Command", "stop");
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startService(i);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //publishProgress(1);
        return title;
    }



    /**
     * 这里的String参数对应AsyncTask中的第三个参数（也就是接收doInBackground的返回值）
     * 在doInBackground方法执行结束之后在运行，并且运行在UI线程当中 可以对UI空间进行设置
     */
    @Override
    protected void onPostExecute(String title) {
        Toast.makeText(context, "onPostExecute", Toast.LENGTH_SHORT).show();
    }


    //该方法运行在UI线程当中,并且运行在UI线程当中 可以对UI空间进行设置
    @Override
    protected void onPreExecute() {
        Toast.makeText(context, "onPreExecute", Toast.LENGTH_SHORT).show();
    }

    /**
     * 这里的Intege参数对应AsyncTask中的第二个参数
     * 在doInBackground方法当中，，每次调用publishProgress方法都会触发onProgressUpdate执行
     * onProgressUpdate是在UI线程中执行，所有可以对UI空间进行操作
     */
    @Override
    protected void onProgressUpdate(Integer... values) {
        int vlaue = values[0];
        Toast.makeText(context, "onProgressUpdate", Toast.LENGTH_SHORT).show();
    }
}
